function varargout=Eyelink2(varargin)
% writes messages to a text file for debugging
% for debug-mode SWITCH: 
% Eyelink('OpenFile' ... to Eyelink2('OpenFile' 
% Eyelink('Message'  ... to Eyelink2('Message' 
% Eyelink2('CloseFile'... to Eyelink2('CloseFile' 
% Eyelink2('Command'  ... to Eyelink2('Command' 
%bcoe
persistent fileID
if Eyelink('IsConnected')==-1 % if InitializeDummy
    switch lower(varargin{1})
        case 'openfile'
           fclose('all');
           if exist([varargin{2}(1:end-3) 'txt'],'file')
            end
            fileID = fopen([varargin{2}(1:end-3) 'txt'],'wt');
            if fileID>0
                fprintf('textfile opened for logging messages\n')
                varargout={0};
            else
                varargout={-1};
            end
            
        case 'message'
           %sprintf('%s\n', varargin{2})
           count=fprintf(fileID,'%s\n', varargin{2}); %#ok<NASGU>
         case 'closefile'
            fclose('all');
            fileID=[];
            fprintf('textfile closed \n');
           
        case 'command'
            if nargout
                varargout={zeros(1,nargout)};
            end
        otherwise% go to actual eyelink
            if nargout
                if isempty(varargin)
                    varargout={Eyelink};
                else
                    varargout={Eyelink(varargin{:})};
                end
            end
    end%switch varargin{1}
    
else% go to actual eyelink
    if nargout
        if isempty(varargin)
            varargout={Eyelink};
        else
            varargout={Eyelink(varargin{:})};
        end
    else
        if isempty(varargin)
            Eyelink;
        else
            Eyelink(varargin{:});
        end
        
    end
end